package com.bms.admin.bussiness.news.web;


import java.util.Date;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.admin.util.RedisUtil;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.common.exception.EnumSvrResult;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.news.entity.News;
import com.bms.common.bussiness.news.service.INewsService;
import com.feilong.core.Validator;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2018-08-15
 */
@RestController
@RequestMapping("news")
@Api(value="News-Api",tags="新闻接口")
public class NewsController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(NewsController.class);
	@Autowired
	private INewsService newsService;

	@Autowired
	private RedisUtil redisUtil;

	@ApiOperation("获取新闻列表")
	@GetMapping("list")
	@RequiresPermissions("news:list")
    public Result list(@ApiIgnore @LoginUser NowUser nowUser,
    				  @RequestParam(defaultValue = "1",required=false)Integer pageNum,
    				  @RequestParam(defaultValue = "20",required=false)Integer pageSize,
    				  Long companyId,String title) {
		EntityWrapper<News> ew = new EntityWrapper<>();
		ew.orderBy("create_time", false);
		if(Validator.isNotNullOrEmpty(companyId) && companyId!=0){
			companyId = nowUser.getCompanyId();
			ew.eq("company_id", companyId);
		}
		if(Validator.isNotNullOrEmpty(title)){
			ew.like("title", title);
		}
		return json(newsService.selectPage(new Page<News>(pageNum,pageSize),ew));
    }

	@ApiOperation("获取新闻选择器")
	@GetMapping("newsSelector")
    public Result selector(@ApiIgnore @LoginUser NowUser nowUser,@RequestParam(required=false) String title) {
		EntityWrapper<News> ew = new EntityWrapper<>();
		if(StringUtils.isNotBlank(title)){
			ew.like("title", title);
		}
		Long companyId = nowUser.getCompanyId();
		if(Validator.isNotNullOrEmpty(companyId) && companyId!=0){
			companyId = nowUser.getCompanyId();
			ew.eq("company_id", companyId);
		}
		return json(newsService.selectPage(new Page<News>(1,20),ew));
    }


	@ApiOperation("获取新闻详情")
	@GetMapping("info/{id}")
	@RequiresPermissions("news:info")
    public Result select(@PathVariable Long id){
		return json(newsService.selectById(id));
    }
	
	@ApiOperation("创建新闻")
	@PostMapping("save")
	@RequiresPermissions("news:save")
	public Result save(@ApiIgnore @LoginUser NowUser nowUser, @RequestBody News news){
		Date now = new Date();
		news.setCreateTime(now);
		news.setCreateUser(nowUser.getUsername());
		news.setUpdateTime(now);
		news.setStatus(1);  //保存待发布
		news.setCompanyId(nowUser.getCompanyId());
		newsService.insert(news);
		return success();
	} 

	@ApiOperation("发布新闻")
	@PostMapping("publish/{id}")
	@RequiresPermissions("news:publish")
	public Result publish(@ApiIgnore @LoginUser NowUser nowUser, @PathVariable Long id){
		News news = newsService.selectById(id);
		if(Validator.isNullOrEmpty(news)){
			return fail(EnumSvrResult.DEFIND_NEWS);
		}
		if(news.getStatus()!=1){
			return fail(EnumSvrResult.ERROR_NEWS_STATUS);
		}
		news.setStatus(2);
		news.setUpdateTime(new Date());
		news.setUpdateUser(nowUser.getUsername());
		newsService.updateById(news);
		return success();
	}
	
	@ApiOperation("下架新闻")
	@GetMapping("takeDown/{id}")
	@RequiresPermissions("news:takenDown")
	public Result takenDown(@ApiIgnore @LoginUser NowUser nowUser, @PathVariable Long id){
		News news = newsService.selectById(id);
		if(Validator.isNullOrEmpty(news)){
			return fail(EnumSvrResult.DEFIND_NEWS);
		}
		if(news.getStatus()!=2){
			return fail(EnumSvrResult.ERROR_NEWS_STATUS);
		}
		
		Set<String> keys = redisUtil.keys("news::NewsController.*");
		log.info("匹配清除的key:{}",keys.toString());
		redisUtil.delete(keys);
		news.setStatus(1);
		news.setUpdateTime(new Date());
		news.setUpdateUser(nowUser.getUsername());
		newsService.updateById(news);
		return success();
	}

	@ApiOperation("修改新闻信息")
	@PostMapping("update")
	@RequiresPermissions("news:update")
	public Result update(@ApiIgnore @LoginUser NowUser nowUser,@RequestBody News match){
		News match2 = newsService.selectById(match.getId());
		if(match2.getStatus()!=1){
			return fail(EnumSvrResult.PUBLISH_ERROR);
		}
		match.setUpdateTime(new Date());
		match.setUpdateUser(nowUser.getUsername());
		newsService.updateById(match);
		return success();
	}

	@ApiOperation("删除新闻")
	@GetMapping("delete/{id}")
	@RequiresPermissions("news:delete")
    public Result delete(@PathVariable Long id) {
		newsService.deleteById(id);
		return success();
    }
	

	@ApiOperation("设置热门新闻")
	@PostMapping("hot/{id}")
	@RequiresPermissions("news:hot")
    public Result hot(@ApiIgnore @LoginUser NowUser nowUser,@PathVariable Long id) {
		News news = newsService.selectById(id);
		if(Validator.isNotNullOrEmpty(news) && news.getStatus()==2 && news.getHot()==2){
			news.setUpdateTime(new Date());
			news.setUpdateUser(nowUser.getUsername());
			news.setHot(1);
			newsService.updateById(news);
		}
		return success();
    }

	@ApiOperation("取消热门新闻")
	@PostMapping("unhot/{id}")
	@RequiresPermissions("news:unhot")
    public Result unhot(@ApiIgnore @LoginUser NowUser nowUser,@PathVariable Long id) {
		News news = newsService.selectById(id);
		if(Validator.isNotNullOrEmpty(news) && news.getStatus()==2 && news.getHot()==1){
			news.setUpdateTime(new Date());
			news.setUpdateUser(nowUser.getUsername());
			news.setHot(2);
			newsService.updateById(news);
		}
		return success();
    }
}

